package pilebis;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.ArrayList;

public class DrawEnvironnement extends Component {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public Graphics2D graph2d;
    public Environnement env;
    public ArrayList<Integer> xdeps;
    public ArrayList<Integer> ydeps;
	
	public DrawEnvironnement(Environnement env) {
		super();
		this.env = env;
		this.xdeps = new ArrayList<Integer>();
		this.ydeps = new ArrayList<Integer>();
		for(int k=0; k<this.env.instances.size(); k++){
			this.xdeps.add(0);
			this.ydeps.add(0);
		}
	}

	public DrawEnvironnement() {
		super();
		this.env = null;
		this.xdeps = new ArrayList<Integer>();
		this.ydeps = new ArrayList<Integer>();
	}
    
	@Override
	public void paint(Graphics g) {
		graph2d = (Graphics2D) g;
		graph2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		drawPoignees();
		drawVars();
		drawLinksPoignee();
		drawLinksVars();
	}
	
	private void drawPoignees() {
		for(int i = 0; i<env.pile.size();i++){
			graph2d.setColor(Color.CYAN);
			graph2d.fillRect(0,i*16,40,16);
			graph2d.setColor(Color.BLUE);
			graph2d.drawRect(0,i*16,40,16);
			String nomvari = "@"+env.pile.get(i).nom;
			graph2d.setColor(Color.BLACK);
			graph2d.drawString(nomvari, 5, i*16+11);
		}
	}
	
	private void drawVars() {
		ArrayList<Integer> cpt = new ArrayList<Integer>();
		for(int k=0; k<env.types.size(); k++){
			cpt.add(0);
		}
		for(int i = 0; i<env.instances.size();i++){
			int row = 0;
			for(int j = 0; j<env.types.size(); j++){
				if(env.types.get(j).nomClasse.equals(env.instances.get(i).type.nomClasse)){
					row = j;
				}
			}
			graph2d.setColor(Color.BLACK);
			int xdep = (row+1)*200+((cpt.get(row))%2)*10;
			int ydep = cpt.get(row)*70 + 20;
			env.instances.get(i).xaff = xdep;
			env.instances.get(i).yaff = ydep;
			cpt.set(row, cpt.get(row)+1);
			graph2d.drawRect(xdep, ydep, 130, 40);
			String nomvari = env.types.get(row).nomClasse+" "+env.instances.get(i).name;
			graph2d.drawString(nomvari, xdep+2, ydep+12);
			if(!env.types.get(row).isPrimitive){
				for(int p=0; p<env.types.get(row).typesAttributs.size(); p++){
					String typeatt = env.types.get(row).typesAttributs.get(p).nomClasse;
					String nomatt = env.types.get(row).nomsAttributs.get(p);
					graph2d.drawString(typeatt+" "+nomatt, xdep+2, ydep+(p+2)*10+7);
				}
			}
		}
	}
	
	private void drawLinksPoignee(){
		for(int i=0; i<this.env.pile.size(); i++){
			if(this.env.pile.get(i).pointeVers!=null){
				String pointname = this.env.pile.get(i).pointeVers.name;
				int rang = 0;
				for(int j=0; j<this.env.instances.size(); j++){
					if(this.env.instances.get(j).name.equals(pointname)){
						rang = j;
					}
				}
				//graph2d.drawLine(41, 8+i*16, this.xdeps.get(rang), this.ydeps.get(rang));
				graph2d.drawLine(41, 8+i*16, this.env.instances.get(rang).xaff, this.env.instances.get(rang).yaff);
			}
		}
	}
	
	private void drawLinksVars() {
		for(int i = 0; i<this.env.instances.size(); i++){
			int xdep = this.env.instances.get(i).xaff + 130;
			//int ydep = this.env.instances.get(i).yaff + 20;
			for(int j=0; j<env.instances.get(i).attributs.size(); j++){
				String nameatt = env.instances.get(i).attributs.get(j).name;
				int rang = 0;
				for(int k=0; k<this.env.instances.size(); k++){
					if(this.env.instances.get(k).name.equals(nameatt)){
						rang = k;
					}
				}
				int ydep = this.env.instances.get(i).yaff+(j+2)*10;
				int xarr = this.env.instances.get(rang).xaff;
				int yarr = this.env.instances.get(rang).yaff;
				graph2d.drawLine(xdep,ydep,xarr,yarr);
			}
		}
	}
	
	@Override
	public Dimension getPreferredSize() {	
		return new Dimension(env.types.size()*260, 600);
	}
	
	/*private void drawBackground(Graphics2D g2) {
		g2.setColor(Color.WHITE);
		Dimension size = getPreferredSize();
		g2.fillRect(0, 0, size.width, size.height);
	}	
	*/
}

